package com.example.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.model.AddAppointmentDTO;
import com.example.model.entity.Appointment;

@Mapper
public interface AppointmentMapper {

    @Insert("insert into tb_appointment(applicantId, approverId, status, appointmentTime, address) values(#{applicantId}, #{approverId}, 'pending', #{appointmentTime}, #{address})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addAppointment(AddAppointmentDTO addAppointmentDTO);

    @Select("select count(*) from tb_appointment where approverId = #{approverId} and DATE(appointmentTime) = CURDATE() and status != 'completed'")
    int haveFreeTime(@Param("approverId") Integer approverId);

    @Select("select id, applicantId, status, DATE_FORMAT(appointmentTime, '%Y-%m-%d %H:%i') as appointmentTime, address, approverId, commented, reback from tb_appointment where applicantId = #{applicantId} order by id desc limit 1000")
    List<Appointment> getListAppointmentByApplicantId(@Param("applicantId") Integer applicantId);

    @Select("select commented from tb_appointment where id = #{orderId}")
    boolean hasCommented(@Param("orderId") Integer orderId);

    @Update("update tb_appointment set status = 'completed' where id = #{id}")
    int completeAppointment(@Param("id") Integer id);

    @Update("update tb_appointment set commented = true where id = #{id}")
    int updateCommented(@Param("id") Integer id);

    @Select("select id, applicantId, status, DATE_FORMAT(appointmentTime, '%Y-%m-%d %H:%i') as appointmentTime, address, approverId, commented, reback from tb_appointment where applicantId = #{applicantId} and status = 'pending'")
    List<Appointment> getListAppointmentInPendingByApplicantId(@Param("applicantId") Integer applicantId);

    @Select("select id, applicantId, status, DATE_FORMAT(appointmentTime, '%Y-%m-%d %H:%i') as appointmentTime, address, approverId, commented from tb_appointment where applicantId = #{applicantId} and status = 'progress'")
    List<Appointment> getListAppointmentInProgressByApplicantId(@Param("applicantId") Integer applicantId);

    @Update("update tb_appointment set reback = true where id = #{id}")
    int updateReback(@Param("id") Integer id);

    @Select("select id, applicantId, status, DATE_FORMAT(appointmentTime, '%Y-%m-%d %H:%i') as appointmentTime, address from tb_appointment where approverId = #{approverId} order by id desc limit 1000")
    List<Appointment> getListAppointmentByApproverId(@Param("approverId") Integer approverId);

    @Update("update tb_appointment set status = 'progress' where id = #{id}")
    int progressAppointment(@Param("id") Integer id);

    @Select("select id, applicantId, status, DATE_FORMAT(appointmentTime, '%Y-%m-%d %H:%i') as appointmentTime, address from tb_appointment where approverId = #{approverId} and DATE(appointmentTime) between #{startDate} and #{endDate} order by id desc limit 1000")
    List<Appointment> getListAppointmentByApproverIdAndDate(@Param("approverId") Integer approverId, @Param("startDate") String startDate, @Param("endDate")String endDate);

    @Select("select approverId from tb_appointment where id = #{id}")
    Integer getApproverIdById(@Param("id") Integer id);
}
